/**
 * @file
 * @brief Miscellaneous utility functions
 */

#ifndef _UTILS_H
#define _UTILS_H

#include <syncupconfig.h>
#include <list.h>
#include <string.h>
#include <glib.h>

#define __UTILS_H_INSIDE__

#ifdef OS_WIN
#include <utils_win.h>
#endif

#undef __UTILS_H_INSIDE__

/**
 * @brief Free all list entries by call g_slice_free()
 */
#define list_g_slice_free(head, type, member) \
    do { \
        struct list_head *__pos, *__t; \
        struct list_head *__head = head; \
        list_for_each_safe(__pos, __t, __head) { \
            list_del(__pos); \
            g_slice_free(type, list_entry(__pos, type, member)); \
        } \
    } while (0)

/**
 * @brief Simple function to get base name of path
 *
 * It doesn't allocate new space.
 */
static inline const char *
get_basename(const char *path)
{
    const char *name = strrchr(path, G_DIR_SEPARATOR);
    if (name == NULL) {
        return path;
    } else {
        return name + 1;
    }
}

/**
 * @brief Convenient function to append path component
 */
static inline void
path_append_component(GString *path, const char *component)
{
    g_string_append_c(path, G_DIR_SEPARATOR);
    g_string_append(path, component);
}

extern int get_sha1sum(const char *path, char *digest);

#endif /* ! _UTILS_H */
